package cn.gogetter.glicense;

import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.prefs.Preferences;

import com.alibaba.fastjson.JSON;

import de.schlichtherle.license.CipherParam;
import de.schlichtherle.license.DefaultCipherParam;
import de.schlichtherle.license.DefaultKeyStoreParam;
import de.schlichtherle.license.DefaultLicenseParam;
import de.schlichtherle.license.KeyStoreParam;
import de.schlichtherle.license.LicenseContent;
import de.schlichtherle.license.LicenseManager;
import de.schlichtherle.license.LicenseParam;
/**
 * 校验License证书类
 * @author liufuwen
 *
 */
public class CheckLicense {
	
	public static void main(String[] args) {
		
		LicenseContent result = null;
		LicenseManager licenseManager  = null;
		try{
			Preferences preference = Preferences
	                .userNodeForPackage(CheckLicense.class);
	        // 设置对证书内容加密的对称密码
	        CipherParam cipherParam = new DefaultCipherParam("ggzn20");
	        // 参数1,2从哪个Class.getResource()获得密钥库;参数3密钥库的别名;参数4密钥库存储密码;参数5密钥库密码
	        KeyStoreParam keyStoreParam = new DefaultKeyStoreParam(
	        		CheckLicense.class, "/license/publicKeys.keystore", "publicKey", "ggzn20", null);
	        LicenseParam licenseParams = new DefaultLicenseParam("demo",
	                preference, keyStoreParam, cipherParam);
	        
            /************** 证书发布者端执行 ******************/
            licenseManager = LicenseManagerHolder
                    .getLicenseManager(licenseParams);
            // install证书时会校验一些默认值，如是否过期
            result = licenseManager.install(new File("D:/license/license.lic"));    
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("证书安装失败!");
           
        }
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if(result != null){
			System.out.println(JSON.toJSONString(result));
			System.out.println("证书安装成功，证书有效期："+format.format(result.getNotBefore())+" - "+format.format(result.getNotAfter()));
			Object extra = result.getExtra(); // 获取扩展信息
			System.out.println(JSON.toJSONString(extra));
			// 然后就是对自定义的扩展信息extra进行校验
		}
			
		
		
		
	}
}
